package com.earl.eatbao.service.impl;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;

import com.earl.eatbao.dao.eatbao.ShopperDao;
import com.earl.eatbao.domain.eatbao.Shopper;
import com.earl.eatbao.util.UpLoadFile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.earl.eatbao.commons.dao.BaseRepsitoty;
import com.earl.eatbao.commons.service.BaseServiceImpl;
import com.earl.eatbao.dao.eatbao.BusinessApplyDao;
import com.earl.eatbao.domain.eatbao.BusinessApply;
import com.earl.eatbao.service.BusinessApplyService;
import org.springframework.web.multipart.MultipartFile;

@Service("bussinessApplyService")
public class BusinessApplyServiceImpl extends BaseServiceImpl<BusinessApply, Long> implements BusinessApplyService {

	// desc:日记工具logger
	private static final Logger logger = LoggerFactory.getLogger(BusinessApplyServiceImpl.class);

	@Resource
	private BusinessApplyDao businessApplyDao;

	@Autowired
	private ShopperDao shopperDao;
	
	@Override
	protected BaseRepsitoty<BusinessApply, Long> getDao() {
		return businessApplyDao;
	}

	@Override
	public void failOnBusinessApply(Long applyId) {
		logger.info("<====== 进入failOnBusinessApply:方法,{[applyId] :" + applyId + " } ====");
		changeBussinessApplyStatus(applyId, 3);
		logger.info("==== 退出failOnBusinessApply方法 返回值类型:{ void: } =======>");
	}

	/**
	 * 
	 * @param applyId
	 */
	@Override
	public void successOnBusinessApply(Long applyId){
		logger.info("<====== 进入successOnBusinessApply:方法,{[applyId] :" + applyId + " } ====");
		changeBussinessApplyStatus(applyId, 2);
		logger.info("==== 退出successOnBusinessApply方法 返回值类型:{ void: } =======>");
	}

	@Override
	public Boolean beBusinessApply(String desc, Long userId, MultipartFile backImg, MultipartFile fontImg) {

		logger.info("<====== 进入beBusinessApply:方法,{[dese, userId] : " +
                desc + ":" +
                userId + "} ====");
		Boolean result = false;

		if (null != userId) {
			BusinessApply businessApply = new BusinessApply();
			Shopper shopper = new Shopper();

			businessApply.setDoce(desc);
			shopper.setIntroduction(desc);

			businessApply.setCreatedBy(String.valueOf(userId));
			shopper.setCreatedBy(userId + "");

			businessApply.setCreatedDate(new Date());
			shopper.setCreatedDate(new Date());

			businessApply.setLastModifiedBy(String.valueOf(userId));
			shopper.setLastModifiedBy(String.valueOf(userId));

			businessApply.setLastModifiedDate(new Date());
			shopper.setLastModifiedDate(new Date());

			businessApply.setStatus(1);
			shopper.setStatus(1);

			businessApply.setDelete(false);
			shopper.setDelete(false);

			businessApply.setUserId(userId);
			shopper.setUserId(userId);

			String backImage = UpLoadFile.saveFile(backImg, UUID.randomUUID().toString());
			businessApply.setIdentityBackImg(backImage);
			shopper.setIdentityBackImg(backImage);

			String fontImage = UpLoadFile.saveFile(fontImg, UUID.randomUUID().toString());
			businessApply.setIdentityImg(fontImage);
			shopper.setIdentityImg(fontImage);

			businessApplyDao.save(businessApply);
			shopperDao.save(shopper);

			result = true;
		} else {
			logger.info("==== 注册商家为空 ====");
		}
		logger.info("==== 退出beBusinessApply方法 返回值类型:{ Boolen :" + result + "} =======>");
        return result;
	}

	@Override
	public List<BusinessApply> findAllWaitApplyBusiness() {
		return businessApplyDao.findBusinessWithStatus(1);
	}

    @Override
    public List<BusinessApply> findAllPassApplyBusiness() {
        return businessApplyDao.findBusinessWithStatus(2);
    }

    @Override
    public List<BusinessApply> findAllFailApplyBusiness() {
        return businessApplyDao.findBusinessWithStatus(3);
    }

    /**
	 * 更改商家注册的状态
	 * @param applyId
	 * @param status
	 */
	private void changeBussinessApplyStatus(Long applyId, int status) {
		BusinessApply businessApply = businessApplyDao.findOne(applyId);
		if (null != businessApply) {
			Shopper shopper = shopperDao.findOneByUserId(businessApply.getUserId());
			if (status == 2) {
				shopper.setStatus(2);
			} else if (status == 3) {
				shopper.setStatus(3);
			}
			businessApply.setStatus(status);

			shopperDao.save(shopper);
			businessApplyDao.save(businessApply);
		} else {
			logger.info("==== 该申请不存在 =====");
		}
	}

}